package cn.edu.scau.cmi.maikejian.client;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.maikejian.adapter.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class AdapterClient {
	Stage primaryStage;
	List<Student> studentList = new ArrayList<Student>();
	Sort classTarget=new SortClassAdapter();
	Sort objectTarget = new SortObjectAdapter();
	Label resultLabel1 = new Label();
	
	
	public AdapterClient() {
		try {
		primaryStage = new Stage();
		GridPane pane = new GridPane();
		Button buttonClass = new Button("add with Ad of class");
		Button buttonObject = new Button("add with Ad of object");
		TextField inputField1 = new TextField();
		TextField inputField2 = new TextField();
		buttonClass.setOnAction(e-> showResult_class(inputField1.getText(), inputField2.getText()));
		buttonObject.setOnAction(e-> showResult_object(inputField1.getText(), inputField2.getText()));
		pane.setAlignment(Pos.CENTER);
		pane.setPadding(new Insets(12,12,12,12));
		pane.setVgap(10);
		pane.setHgap(10);
		pane.add(new Label("Number:"), 0, 0);
		pane.add(new Label("Name:"), 0, 1);
		pane.add(inputField1, 1, 0);
		pane.add(inputField2, 1, 1);
		pane.add(buttonClass, 0, 2);
		pane.add(buttonObject, 1, 2);
		pane.add(resultLabel1, 1, 3);
		Scene scene = new Scene(pane, 600, 600);
		primaryStage.setTitle("Adapter");
		primaryStage.setScene(scene);
		primaryStage.show();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void showResult_class(String number, String name) {
		int num = Integer.parseInt(number);
		Student student = new Student(num, name);
		studentList.add(student);
		studentList = classTarget.sortStudents(studentList);
		String objectString;
		StringBuilder stringBuilder = new StringBuilder();
		for(Student studenti : studentList) {
			stringBuilder.append(studenti.getId() + " " + studenti.getName() + "\n");
		}
		objectString = stringBuilder.toString();
		resultLabel1.setText(objectString);
	}
	
	private void showResult_object(String number, String name) {
		int num = Integer.parseInt(number);
		Student student = new Student(num, name);
		studentList.add(student);
		studentList = classTarget.sortStudents(studentList);
		String objectString;
		StringBuilder stringBuilder = new StringBuilder();
		for(Student studenti : studentList) {
			stringBuilder.append(studenti.getId() + " " + studenti.getName() + "\n");
		}
		objectString = stringBuilder.toString();
		resultLabel1.setText(objectString);
	}
	
}